package com.jason.www.dao;

import com.jason.www.domain.Roles;
import com.jason.www.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Set;

public interface UserMapper {
    @Delete({
        "delete from user",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into user (id, user_name, ",
        "user_age, user_sex, ",
        "user_birth, login_name, ",
        "login_password)",
        "values (#{id,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, ",
        "#{userAge,jdbcType=INTEGER}, #{userSex,jdbcType=VARCHAR}, ",
        "#{userBirth,jdbcType=DATE}, #{loginName,jdbcType=VARCHAR}, ",
        "#{loginPassword,jdbcType=VARCHAR})"
    })
    int insert(User record);

    @InsertProvider(type=UserSqlProvider.class, method="insertSelective")
    int insertSelective(User record);

    @UpdateProvider(type=UserSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(User record);

    @Update({
            "update user",
            "set user_name = #{userName,jdbcType=VARCHAR},",
            "user_age = #{userAge,jdbcType=INTEGER},",
            "user_sex = #{userSex,jdbcType=VARCHAR},",
            "user_birth = #{userBirth,jdbcType=DATE},",
            "login_name = #{loginName,jdbcType=VARCHAR},",
            "login_password = #{loginPassword,jdbcType=VARCHAR}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(User record);

    @Select({
        "select",
        "id, user_name, user_age, user_sex, user_birth, login_name, login_password",
        "from user",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_age", property="userAge", jdbcType=JdbcType.INTEGER),
        @Result(column="user_sex", property="userSex", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_birth", property="userBirth", jdbcType=JdbcType.DATE),
        @Result(column="login_name", property="loginName", jdbcType=JdbcType.VARCHAR),
        @Result(column="login_password", property="loginPassword", jdbcType=JdbcType.VARCHAR)
    })
    User selectByPrimaryKey(Long id);

    @Select({
            "select",
            "id, user_name, user_age, user_sex, user_birth, login_name, login_password",
            "from user"
    })
    @Results(id = "userResultMap" ,
            value = {
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_age", property="userAge", jdbcType=JdbcType.INTEGER),
            @Result(column="user_sex", property="userSex", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_birth", property="userBirth", jdbcType=JdbcType.DATE),
            @Result(column="login_name", property="loginName", jdbcType=JdbcType.VARCHAR),
            @Result(column="login_password", property="loginPassword", jdbcType=JdbcType.VARCHAR)
    })
    List<User> getAllUser();

    @Select({
            "SELECT r.* FROM USER u,user_roles ur,roles r " +
                    "WHERE u.`id`=ur.`user_id` " +
                    "AND r.`id`=ur.`roles_id` " +
                    "AND u.user_name = #{loginName}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="theurl", property="theurl", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR)
    })
    Set<Roles> getRolesByLoginName(String loginName);


    @Select({
            "select",
            "id, user_name, user_age, user_sex, user_birth, login_name, login_password",
            "from user where user_name=#{username}"
    })
    @ResultMap("userResultMap")
    User getUserByUsername(String username);

    @Select({
            "select",
            "id, user_name, user_age, user_sex, user_birth, login_name, login_password",
            "from user where login_name=#{username}"
    })
    @ResultMap("userResultMap")
    User getUserByLoginName(String loginName);
}